<?xml version="1.0" encoding="UTF-8" ?>

<!--
    Document   : convertCSV2AEA.xsl
    Created on : 13. August 2007, 22:12
    Author     : jtrapp
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="accountname">Changeme</xsl:param>
    <xsl:param name="date">2007-08-01T09:13:29Z</xsl:param>
    <!-- TODO customize transformation rules 
         syntax recommendation http://www.w3.org/TR/xslt 
    -->
    <xsl:template match="/">
        <ae-data type="data" version="1.4" xmlns="http://services.google.com/adwordseditor/">
        <accounts><account>
<xsl:attribute name="name"><xsl:value-of select="$accountname"/></xsl:attribute>
<xsl:attribute name="lastupdate"><xsl:value-of select="$date"/></xsl:attribute><campaigns>
        <xsl:apply-templates select="/table" mode="campaign"/>
        </campaigns></account></accounts></ae-data>
    </xsl:template>
    
    <xsl:template match="table" mode="campaign">
      <xsl:for-each select="row[not (cell[@name='Campaign']=preceding-sibling::row/cell[@name='Campaign'])]">
                <campaign>
                    <!--xsl:attribute name="name"><xsl:value-of select="cell[@name='Campaign']"/></xsl:attribute-->
                    <xsl:call-template name="campaign">
                        <xsl:with-param name="campaignname" select="cell[@name='Campaign']"/>
                    </xsl:call-template>
                </campaign>
            </xsl:for-each>
    </xsl:template>
        

    
    <xsl:template name="campaign">       
        <xsl:param name="campaignname"/>
        <xsl:text>  </xsl:text><campaign-data>
            <name><xsl:value-of select="$campaignname"/></name>
            <dailyBudget><xsl:value-of select="number(cell[starts-with(@name,'Campaign Daily Budget')])*1000000"/></dailyBudget>
            <status><xsl:value-of select="cell[@name='Campaign Status']"/></status>
            <endDate>2037-12-30</endDate>
        </campaign-data>
            
            <criteria negative="true"><keywords>
            <xsl:apply-templates select="parent::node()/row[cell[@name='Campaign']=$campaignname][cell[@name='Ad Group']=''][cell[@name='Report Type']='Keyword'][cell[@name='Keyword Type']='Campaign Negative']" mode="campaignnegatives"/>
            </keywords></criteria>

            <adgroups>
                <xsl:for-each select="parent::node()/row[not(cell[@name='Ad Group']='')][cell[@name='Campaign']=$campaignname]">
                    <xsl:if test="not (cell[@name='Ad Group']=preceding-sibling::row[cell[@name='Campaign']=$campaignname]/cell[@name='Ad Group'])">
                        <adgroup>
                            <!--xsl:attribute name="name"><xsl:value-of select="cell[@name='Campaign']"/></xsl:attribute-->
                            <xsl:call-template name="adgroup">
                                <xsl:with-param name="campaignname" select="$campaignname"/>
                                <xsl:with-param name="adgroupname" select="cell[@name='Ad Group']"/>                        
                            </xsl:call-template>
                        </adgroup>
                    </xsl:if>
                </xsl:for-each>
            </adgroups>
    </xsl:template>
    

     <xsl:template match="row" mode="campaignnegatives">
        <xsl:text>  </xsl:text>
        <keyword>
               <xsl:text>  </xsl:text><keyword-data>
                   <text><xsl:value-of select="cell[@name='Keyword']"/></text><type>Broad</type>
                </keyword-data>
         </keyword>
    </xsl:template>
    
    
    
 <xsl:template name="adgroup">
        <xsl:param name="campaignname"/>
        <xsl:param name="adgroupname"/>
         <adgroup-data>
                <name><xsl:value-of select="$adgroupname"/></name>
                <maxCpc><xsl:value-of select="number(cell[@name='Max CPC(EUR)'])*1000000"/></maxCpc>
                <maxContentCpc><xsl:value-of select="number(cell[@name='Max Content CPC(EUR)'])*1000000"/></maxContentCpc>
                <status><xsl:choose><xsl:when test="cell[@name='AdGroup Status']='Active'">Enabled</xsl:when><xsl:otherwise><xsl:value-of select="cell[@name='AdGroup Status']"/></xsl:otherwise></xsl:choose></status>
            </adgroup-data>
            <creatives>
                    <textads>
                            <xsl:for-each select="parent::node()/row[cell[@name='Campaign']=$campaignname][cell[@name='Ad Group']=$adgroupname][cell[@name='Report Type']='Ad Text']">
                                <textad>
                                    <!--xsl:attribute name="name"><xsl:value-of select="cell[@name='Campaign']"/></xsl:attribute-->
                                    <xsl:call-template name="textad">
                                        <xsl:with-param name="campaignname" select="$campaignname"/>
                                        <xsl:with-param name="adgroupname" select="$adgroupname"/>   
                                        <xsl:with-param name="adtextheadline" select="cell[@name='Headline']"/>
                                    </xsl:call-template>
                                </textad>
                            </xsl:for-each>
                    </textads>
            </creatives>
            <criteria negative="true"><keywords>
            <xsl:apply-templates select="parent::node()/row[cell[@name='Campaign']=$campaignname][cell[@name='Ad Group']=$adgroupname][cell[@name='Report Type']='Keyword'][cell[@name='Keyword Type']='Negative']" mode="keywordnegative"/>
            </keywords></criteria> 
            
            <criteria negative="false"><keywords>
            <xsl:apply-templates select="parent::node()/row[cell[@name='Campaign']=$campaignname][cell[@name='Ad Group']=$adgroupname][cell[@name='Report Type']='Keyword'][not(cell[@name='Keyword Type']='Negative')]" mode="keywordpositive"/>
            </keywords></criteria>
     </xsl:template>

     <xsl:template name="textad">
        <xsl:param name="campaignname"/>
        <xsl:param name="adgroupname"/>
        <xsl:param name="adtextheadline"/>
        <textad-data>
                <xsl:text>  </xsl:text><headline><xsl:value-of select="$adtextheadline"/></headline>
                <description1><xsl:value-of select="cell[@name='Description Line 1']"/></description1>
                <description2><xsl:value-of select="cell[@name='Description Line 2']"/></description2>
                <destinationUrl><xsl:value-of select="cell[@name='Destination URL']"/></destinationUrl>
                <displayUrl><xsl:value-of select="cell[@name='Display URL']"/></displayUrl>
<status><xsl:choose><xsl:when test="cell[@name='Creative Status']='Active'">Enabled</xsl:when><xsl:otherwise><xsl:value-of select="cell[@name='Creative Status']"/></xsl:otherwise></xsl:choose></status>
            </textad-data>
        </xsl:template>
        
        <xsl:template match="row" mode="keywordpositive">
           <keyword>
               <keyword-data>
                   <text><xsl:value-of select="cell[@name='Keyword']"/></text><type><xsl:value-of select="cell[@name='Keyword Type']"/></type>
<paused><xsl:choose><xsl:when test="cell[@name='Creative Status']='Active'">false</xsl:when><xsl:otherwise>true</xsl:otherwise></xsl:choose></paused>
                </keyword-data>
           </keyword>
    </xsl:template>
             <xsl:template match="row" mode="keywordnegative">
           <keyword>
               <keyword-data>
                   <text><xsl:value-of select="cell[@name='Keyword']"/></text><type>Broad</type>
                </keyword-data>
           </keyword>
    </xsl:template>
    
    <xsl:template match="cell" mode="campaign">
            <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>
